package org.musicPlayerServer.business.object;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Folder extends Content{

    
    public Folder(String folder) {
	super(folder);
    }

    public List<Content> listContent() {
	List<Content> result = new ArrayList<Content>();
	File[] fileArray = file.listFiles();
	Arrays.sort(fileArray);
	List<File> fileList = Arrays.asList(fileArray);
	for(File afile : fileList){
	    if(afile.isDirectory()&&!afile.getName().startsWith(".")){
		Folder f = new Folder(afile.getAbsolutePath());
		result.add(f);
	    }else if(afile.getName().endsWith(".mp3")){
		Music m = new Music(afile.getAbsolutePath());
		result.add(m);
	    }else if(afile.getName().toLowerCase().endsWith(".jpg")){
		Image i = new Image(afile.getAbsolutePath());
		result.add(i);
	    }
	}
	
	return result;
    }

    public Folder getParentFolder(Folder currentFolder) {
	// TODO Auto-generated method stub
	return new Folder(new File(currentFolder.getFilePath()).getParent());
    }
    
    public String toHtml(String contextPath) {
	return "<a href=\""+contextPath+"?action=list&folder="+file.getAbsolutePath()+"\" >"+file.getName()+"</a>";
    }
    
    
    

}
